from xml.dom import minidom
from structs import Selector,Field,FieldType,Customer,Menu,DicElement,Special
from core.settings import WEB_ROOT

class PathUtil:
    
    @staticmethod
    def tran(path):
        return WEB_ROOT + '/config/' + path

class SelectorReader :

    @staticmethod
    def read(path,mgr):
        doc = minidom.parse(PathUtil.tran(path))
        for node in doc.childNodes[0].childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'select':
                selectid = int(node.attributes['id'].nodeValue)
                selectname = node.attributes['name'].nodeValue
                selector = Selector(selectid,selectname)
                for option in node.childNodes :
                    if option.nodeType == node.ELEMENT_NODE and option.nodeName == 'option':
                        optionvalue = int(option.attributes['value'].nodeValue)
                        subdir = None
                        if option.attributes.has_key('subdir'):
                            subdir = option.attributes['subdir'].nodeValue
                        optiondesc = option.childNodes[0].nodeValue
                        selector.put(optionvalue, optiondesc,subdir)
                mgr.putSelector(selector)

class CustomerReader:

    @staticmethod
    def read(name,path,mgr):
        print path
        doc = minidom.parse(PathUtil.tran(path))
        customer = Customer(name,path)
        for node in doc.childNodes[0].childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'field':
                fieldname = node.attributes['name'].nodeValue
                fieldtype = node.attributes['type'].nodeValue
                fielddesc = node.attributes['desc'].nodeValue
                rawtype = mgr.getRawFieldType(fieldtype)
                if rawtype == FieldType.List :
                    param = node.attributes['customer'].nodeValue
                    refcustomer = mgr.getCustomer(param)
                    field = Field(fieldname,rawtype,fielddesc,refcustomer)
                elif rawtype == FieldType.Select :
                    param = int(node.attributes['selectid'].nodeValue)
                    selector = mgr.getSelector(param)
                    field = Field(fieldname,rawtype,fielddesc,selector)
                elif rawtype == FieldType.Customer:
                    param = node.attributes['customer'].nodeValue
                    refcustomer = mgr.getCustomer(param)
                    field = Field(fieldname,rawtype,fielddesc,refcustomer)
                elif rawtype == FieldType.SimpleList:
                    param = node.attributes['customer'].nodeValue
                    listtype = mgr.getRawFieldType(param)
                    field = Field(fieldname,rawtype,fielddesc,listtype)
                elif rawtype == FieldType.Special:
                    param = node.attributes['customer'].nodeValue
                    field = Field(fieldname,rawtype,fielddesc,param)
                elif rawtype == FieldType.SelectList:
                    param = node.attributes['customer'].nodeValue
                    field = Field(fieldname,rawtype,fielddesc,param)
                elif rawtype == FieldType.String and node.attributes.has_key('lang') and node.attributes['lang'].nodeValue == 'true':
                    field = Field(fieldname,rawtype,fielddesc,lang=True)
                else:
                    field = Field(fieldname,rawtype,fielddesc)
                customer.putField(field)
        mgr.putCustomer(customer)

class TypesReader:
    
    @staticmethod
    def read(path,mgr):
        doc = minidom.parse(PathUtil.tran(path))
        for node in doc.childNodes[0].childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'customer':
                typename = node.attributes['name'].nodeValue
                typepath = node.childNodes[0].nodeValue
                CustomerReader.read(typename, typepath, mgr)

class SpecialReader:
    @staticmethod
    def read(path,mgr):
        print path
        doc = minidom.parse(PathUtil.tran(path))
        for node in doc.childNodes[0].childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'special':
                specialname = node.attributes['name'].nodeValue
                specialdesc = node.attributes['desc'].nodeValue
                for subnode in node.childNodes:
                    if subnode.nodeType == node.ELEMENT_NODE:
                        if subnode.nodeName == 'getlist':
                            getlistsql = subnode.childNodes[0].nodeValue
                        elif subnode.nodeName == 'get':
                            getsql = subnode.childNodes[0].nodeValue
                special = Special(specialname,specialdesc,getlistsql,getsql)
                mgr.putSpecial(special)

class DicReader:

    @staticmethod
    def read(menu,tabid,name,key,seq,path,upload,deploy,mgr):
        print path
        doc = minidom.parse(PathUtil.tran(path))
        dic = DicElement(name,path,key,seq,upload,deploy)
        for node in doc.childNodes[0].childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'field':
                fieldname = node.attributes['name'].nodeValue
                fieldtype = node.attributes['type'].nodeValue
                fielddesc = node.attributes['desc'].nodeValue
                rawtype = mgr.getRawFieldType(fieldtype)
                if rawtype == FieldType.List :
                    param = node.attributes['customer'].nodeValue
                    refcustomer = mgr.getCustomer(param)
                    fieldmaxlen = 500
                    if node.attributes.has_key('maxlen'):
                        fieldmaxlen = int(node.attributes['maxlen'].nodeValue)
                    field = Field(fieldname,rawtype,fielddesc,refcustomer,maxlen=fieldmaxlen)
                elif rawtype == FieldType.Select :
                    param = int(node.attributes['selectid'].nodeValue)
                    selector = mgr.getSelector(param)
                    field = Field(fieldname,rawtype,fielddesc,selector)
                elif rawtype == FieldType.Customer:
                    param = node.attributes['customer'].nodeValue
                    refcustomer = mgr.getCustomer(param)
                    fieldmaxlen = 500
                    if node.attributes.has_key('maxlen'):
                        fieldmaxlen = int(node.attributes['maxlen'])
                    field = Field(fieldname,rawtype,fielddesc,refcustomer,maxlen = fieldmaxlen)
                elif rawtype == FieldType.SimpleList:
                    param = node.attributes['customer'].nodeValue
                    listtype = mgr.getRawFieldType(param)
                    field = Field(fieldname,rawtype,fielddesc,listtype)
                elif rawtype == FieldType.Special:
                    param = node.attributes['customer'].nodeValue
                    field = Field(fieldname,rawtype,fielddesc,param)
                elif rawtype == FieldType.SelectList:
                    param = node.attributes['customer'].nodeValue
                    field = Field(fieldname,rawtype,fielddesc,param)
                elif rawtype == FieldType.String:
                    fieldlang = False
                    if node.attributes.has_key('lang') and node.attributes['lang'].nodeValue == 'true':
                        fieldlang = True
                    fieldmaxlen = 500
                    if node.attributes.has_key('maxlen'):
                        fieldmaxlen = int(node.attributes['maxlen'].nodeValue)
                    field = Field(fieldname,rawtype,fielddesc,lang=fieldlang,maxlen = fieldmaxlen)
                elif rawtype == FieldType.Resource:
                    dependfield = ""
                    if node.attributes.has_key('depend') :
                        dependfield = node.attributes['depend'].nodeValue
                    field = Field(fieldname,rawtype,fielddesc,depend=dependfield)
                else:
                    field = Field(fieldname,rawtype,fielddesc)
                dic.putField(field)
        menu.putElement(dic)

class MenuReader:
    
    @staticmethod
    def read(path,mgr):
        print path
        doc = minidom.parse(PathUtil.tran(path))
        for node in doc.childNodes[0].childNodes:
            if node.nodeType == node.ELEMENT_NODE and node.nodeName == 'menu':
                menuid = node.attributes['id'].nodeValue
                menuname = node.attributes['name'].nodeValue
                menu = Menu(menuid,menuname)
                mgr.putMenu(menu)
                for tabnode in node.childNodes:
                    if tabnode.nodeType == node.ELEMENT_NODE and tabnode.nodeName == 'menutab':
                        tabid = tabnode.attributes['id'].nodeValue
                        tabname = tabnode.attributes['name'].nodeValue
                        tabkey = tabnode.attributes['key'].nodeValue
                        tabseq = tabnode.attributes['seq'].nodeValue
                        tabupload = None
                        if tabnode.attributes.has_key('upload') :
                            tabupload = tabnode.attributes['upload'].nodeValue
                        tabdeploy = True
                        if tabnode.attributes.has_key('deploy') and tabnode.attributes['deploy'].nodeValue == 'false':
                            tabdeploy = False
                        tabpath = tabnode.childNodes[0].nodeValue
                        DicReader.read(menu,tabid, tabname, tabkey,tabseq,tabpath,tabupload,tabdeploy,mgr)
